package com.hbx.controller;


import com.hbx.aspect.annotation.SensitiveFilter;
import com.hbx.constant.ResponseStateConstant;
import com.hbx.dto.resource.PushResDto;
import com.hbx.dto.resource.SearchResDto;
import com.hbx.service.ResourceService;
import com.hbx.utils.Result;
import com.hbx.vo.res.MyResVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-15
 */
@RestController
@RequestMapping("/resource")
@Api(tags = "资源处理器")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @RequestMapping(value = "/pushResource",method = RequestMethod.POST)
    @ApiOperation("发布资源")
    @SensitiveFilter
    public Result pushResource(@RequestBody @Valid PushResDto pushResDto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        resourceService.pushResource(pushResDto, r);
        return r;
    }

    /**
     * 获取资源列表
     * @param search
     * @return
     */
    @RequestMapping(value = "/getResourceList",method = RequestMethod.POST)
    @ApiOperation("分页获取资源列表")
    public Result getResourceList(@RequestBody SearchResDto search){
        Result r = new Result();
        if (ObjectUtils.isEmpty(search)){
            r.setCode(400).setMsg("请求参数异常");
            return r;
        }
        if (search.getPageNum() == null){
            search.setPageNum(1);
        }
        if (search.getPageSize() == null){
            r.setCode(400).setMsg("pageSize为必传项");
            return r;
        }
        resourceService.getResourceList(search, r);
        return r;
    }

    /**
     * 查询我的资源
     * @param pageSize
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getMyResource",method = RequestMethod.GET)
    @ApiOperation("查询我的资源")
    public Result<MyResVo> getMyResource(@RequestParam("pageSize") Integer pageSize,
                                         @RequestParam("userId") Integer userId){
        Result<MyResVo> r = new Result<>();
        if (ObjectUtils.isEmpty(pageSize)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("查询条数为必传项");
            return r;
        }
        if (ObjectUtils.isEmpty(userId)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("userId为必传项");
            return r;
        }
        resourceService.getMyResource(pageSize, userId ,r);
        return r;
    }

    /**
     * 删除我的资源
     * @param resId
     * @return
     */
    @RequestMapping(value = "/removeRes",method = RequestMethod.GET)
    @ApiOperation("删除我的资源")
    public Result removeRes(@RequestParam("resId") Integer resId){
        Result r = new Result<>();
        if (ObjectUtils.isEmpty(resId)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("resId为必传项");
            return r;
        }
        resourceService.removeRes(resId, r);
        return r;
    }

    /**
     * 删除我的资源
     * @param resId
     * @return
     */
    @RequestMapping(value = "/getResDetail",method = RequestMethod.GET)
    @ApiOperation("获取资源详情")
    public Result getResDetail(@RequestParam("resId") Integer resId){
        Result r = new Result<>();
        if (ObjectUtils.isEmpty(resId)){
            r.setCode(ResponseStateConstant.PARAMETER_ERROR);
            r.setMsg("resId为必传项");
            return r;
        }
        resourceService.getResDetail(resId, r);
        return r;
    }


}

